{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/embeddings/clarifai.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Embeddings with Clarifai\n",
    "\n",
    "LlamaIndex has support for Clarifai embeddings models."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You must have a Clarifai account and a Personal Access Token (PAT) key. \n",
    "[Check here](https://clarifai.com/settings/security) to get or create a PAT.\n",
    "\n",
    "Set CLARIFAI_PAT as an environment variable or You can pass PAT as argument to ClarifaiEmbedding class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-embeddings-clarifai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!export CLARIFAI_PAT=YOUR_KEY"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install llama-index"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Models can be referenced either by the full URL or by the model_name, user ID, and app ID combination."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.embeddings.clarifai import ClarifaiEmbedding\n",
    "\n",
    "# Create a clarifai embedding class just with model_url, assuming that CLARIFAI_PAT is set as an environment variable\n",
    "embed_model = ClarifaiEmbedding(\n",
    "    model_url=\"https://clarifai.com/clarifai/main/models/BAAI-bge-base-en\"\n",
    ")\n",
    "\n",
    "# Alternatively you can initialize the class with model_name, user_id, app_id and pat as well.\n",
    "embed_model = ClarifaiEmbedding(\n",
    "    model_name=\"BAAI-bge-base-en\",\n",
    "    user_id=\"clarifai\",\n",
    "    app_id=\"main\",\n",
    "    pat=CLARIFAI_PAT,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "embeddings = embed_model.get_text_embedding(\"Hello World!\")\n",
    "print(len(embeddings))\n",
    "print(embeddings[:5])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Embed list of texts "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"roses are red violets are blue.\"\n",
    "text2 = \"Make hay while the sun shines.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "embeddings = embed_model._get_text_embeddings([text2, text])\n",
    "print(len(embeddings))\n",
    "print(embeddings[0][:5])\n",
    "print(embeddings[1][:5])"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
